package com.thzy.bbs.dao;

import com.thzy.bbs.entity.MessageNumber;
import com.thzy.bbs.entity.User;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * 用户实体
 *
 * @author
 * @date 2017/2/26 14:32
 */

public class UserDao {
    private DbManager dbManager = DbInit.init();

    public User findById(Integer id) {
        try {
            return dbManager.findById(User.class, id);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void saveOrUpdate(User user) {
        try {
            dbManager.saveOrUpdate(user);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<User> findAll() {
        try {
            return dbManager.findAll(User.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param columnNames 修改字段名
     */
    public void update(String... columnNames) {
        try {
            dbManager.update(MessageNumber.class, columnNames);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 时间降序获取第一个用户
     *
     * @return
     */
    public User findFirstByCreateTimeDesc() {
        try {
            return dbManager.selector(User.class).orderBy("createTime", true).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
}
